﻿using System;
using System.Text;

using mfRCF.Core;
using System.Net;

namespace mfRCF.netmf
{
    public class mfHttpListener : mfIHttpListener
    {


        #region CTOR
        public mfHttpListener(int port, string protocolPrefix, mfIHttpAuthenticator authtenticator) 
        {
            mfListener = new HttpListener(protocolPrefix, port);

            Port = port;
            ProtocolPrefix = protocolPrefix;
            Authtenticator = authtenticator;
        }
        #endregion

        #region Properties

        public HttpListener mfListener { get; set; }

        #endregion

        #region mfIHttpListener

        public mfIHttpAuthenticator Authtenticator
        {
            get;
            private set;
        }

        public int Port
        {
            get;
            private set;
        }

        public string ProtocolPrefix
        {
            get;
            private set;
        }

        public bool IsListening 
        {
            get { return mfListener.IsListening; }
        }

        public mfIHttpContext Context
        {
            get 
            { 
                var context =  mfListener.GetContext();
                if (context != null)
                {
                    return new mfHttpContext(context, Authtenticator);
                }
                
                throw new mfRcfException("no context available");
            }
        }

        public void Start()
        {
            mfListener.Start();
        }

        public void Stop()
        {
            mfListener.Stop();
        }

       
        #endregion
    }
}
